package com.heima.datastructure.deque;

import org.springframework.validation.annotation.ValidationAnnotationUtils;

import java.util.Iterator;

// 为了方便尾节点的删除也便利，使用双向链表，又为了节省一个哨兵节点的空间，采用环形链表

/**
 * <h3>基于双向环形链表实现双端队列</h3>
 * @author 勾新杰
 * @version 1.0
 * @date 2024/9/1 21:19
 */
public class LinkedListDeque<E> implements Deque<E>, Iterable<E> {
    private final Node<E> sentinel; // 哨兵节点，环形中的一个固定存在的节点
    private int capacity; // 双端队列的容量大小

    private int size; // 双端队列的大小

    {
        sentinel = new Node<>(null,null,null);
        sentinel.next = sentinel;
        sentinel.prev = sentinel;
        size = 0;
    }

    /**
     * 用构造方法初始化双端队列
     */
    public LinkedListDeque() {
        this.capacity = Integer.MAX_VALUE;
    }

    /**
     * 用构造方法初始化双端队列
     * @param capacity 容量
     */
    public LinkedListDeque(int capacity) {
        this.capacity = capacity;
    }

    /**
     * 节点类
     * @param <E>
     */
    private static class Node<E> {
        E value;
        Node<E> prev;
        Node<E> next;

        public Node(E value, Node<E> prev, Node<E> next) {
            this.value = value;
            this.prev = prev;
            this.next = next;
        }
    }

    /**
     * 双端队列左端入队
     * @param value 待入队的值
     * @return 入队成功返回true
     */
    @Override
    public boolean offerFirst(E value) {
        if (isFull()){
            return false;
        }
        Node<E> head = sentinel.next;
        Node<E> offered = new Node<>(value,sentinel,head);
        sentinel.next = offered;
        head.prev = offered;
        size++;
        return true;
    }

    /**
     * 双端队列右端入队
     * @param value 待入队的值
     * @return 入队成功返回true
     */
    @Override
    public boolean offerLast(E value) {
        if (isFull()){
            return false;
        }
        Node<E> tail = sentinel.prev;
        Node<E> offered = new Node<>(value, tail, sentinel);
        tail.next = offered;
        sentinel.prev = offered;
        size++;
        return true;
    }

    /**
     * 获取双端队列左端队头节点的值并删除
     * @return 双端队列左端队头节点的值
     */
    @Override
    public E pollFirst() {
        if (isEmpty()){
            return null;
        }
        Node<E> polled = sentinel.next;
        Node<E> next = polled.next;
        sentinel.next = next;
        next.prev = sentinel;
        size--;
        return polled.value;
    }

    /**
     * 获取双端队列右端队头节点的值并删除
     * @return 双端队列右端队头节点的值
     */
    @Override
    public E pollLast() {
        if (isEmpty()){
            return null;
        }
        Node<E> polled = sentinel.prev;
        Node<E> prev = polled.prev;
        prev.next = sentinel;
        sentinel.prev = prev;
        size--;
        return polled.value;
    }

    /**
     * 获取双端队列左端队头节点的值
     * @return 双端队列左端队头节点的值
     */
    @Override
    public E peekFirst() {
        if (isEmpty()){
            return null;
        }
        return sentinel.next.value;
    }

    /**
     * 获取双端队列右端队头节点的值
     * @return 双端队列右端队头节点的值
     */
    @Override
    public E peekLast() {
        if (isEmpty()){
            return null;
        }
        return sentinel.prev.value;
    }

    /**
     * 判断是否为空
     * @return 为空返回true
     */
    @Override
    public boolean isEmpty() {
        return size == 0;
    }

    /**
     * 判断是否为满
     * @return 为满返回true
     */
    @Override
    public boolean isFull() {
        return size == capacity;
    }

    /**
     * 获取双端队列的大小
     * @return 双端队列的大小
     */
    @Override
    public int size() {
        return size;
    }

    @Override
    public Iterator<E> iterator() {
        return new Iterator<>() {
            Node<E> p = sentinel.next;
            @Override
            public boolean hasNext() {
                return p != sentinel;
            }

            @Override
            public E next() {
                E value = p.value;
                p = p.next;
                return value;
            }
        };
    }

    /**
     * 扩容
     * @param capacity 容量
     * @throws IllegalArgumentException 传入的容量比现有的容量小
     */
    public void expansion(int capacity){
        if (capacity < this.capacity){
            throw new IllegalArgumentException("参数" + capacity + "不能比" + this.capacity + "小");
        }
        this.capacity = capacity;
    }
}
